<template>
	<page-body>
		<view class="page">
			<view class="flex benben-position-layout flex flex-wrap align-center mance_flex_0"
				:style="{height:(88+StatusBarRpx)+'rpx',paddingTop:StatusBarRpx+'rpx', }">
				<view class='flex flex-wrap align-center justify-between flex-sub mance_fd0_0'>
					<view class='flex flex-wrap align-center mance_fd0_0_c0'>
						<text class='fu-iconfont2  mance_fd0_0_c0_c0' @tap.stop="handleJumpDiy" data-type="back"
							data-url="1">&#xE794;</text>
					</view>
					<view class='flex flex-wrap align-stretch justify-center'>
						<text class='mance_fd0_0_c1_c0'>章节</text>
					</view>
					<view class='flex flex-wrap align-center justify-end mance_fd0_0_c0'>
						<text v-if="index !== idList.length-1" @click="nextSection"> 下一节</text>

					</view>
				</view>
			</view>

			<view class="overview" :style="{marginTop:(98+StatusBarRpx)+'rpx'}">
				<div class="title">
					<image class="img-icon" src="@/static/img02/13.png" alt="" /> 学习资源
				</div>
				<view class="resources-outer">
					<view @click="goLook(item)" class="resources-item" v-for="item in list" :key="item.aid">
						<image :src="item.cover" class="item-img"></image>
						<view class="title">
							{{item.resource_name}}
						</view>
						<view class="jindu">
							学习进度 <span>{{item.rate}}%</span>
						</view>
						<view class="type">
							{{item.fileType}}
						</view>
					</view>

					<view class="public-page-empty" v-if="list.length == 0">
						<image src="@/static/global/order.png"></image>
					</view>
				</view>
			</view>
			<!---flex布局flex布局结束-->
			<view class="flex benben-position-layout flex flex-wrap align-center coursePro_flex_3"
				:style=" {height:(110+bottomSafeAreaRpx)+'rpx',paddingBottom:bottomSafeAreaRpx+'rpx', }">
				<button class='coursePro_fd3_0' @tap.stop="startclassFunc()">练习题库</button>
			</view>
			<view :style="{height: (110+bottomSafeAreaRpx)+'rpx'}"></view>
		</view>
	</page-body>
</template>


<script>
	export default {
		data() {
			return {
				list: [],
				idList: [],
				aid: "",
				index: 0
			};
		},
		onLoad(options) {
			this.idList = options.aidList.split(',').map(Number)

		},
		onShow() {
			this.get_course_chapterApi()
		},
		mounted() {},
		methods: {
			async goLook(val) {
				if (val.file_type == 'video') {
					uni.navigateTo({
						url: `/pages/course/scourseStudy/scourseStudy?url=${val.file_path[0].path}&file_type=${val.file_type}&aid=${val.aid}&rate=${val.rate}`
					})
				} else {
					let res = await this.$api.post(global.apiUrls.post680f48d5c8263, {
						freecourseresourcelibrary_id: val.aid,
						duration: 100
					})
					this.seePdf(val.resource_file_path, val.aid)
					// window.open(val.file_path[0].path);
				}
			},
			seePdf(file, aid, item) {
				// console.log('vvvvvvvvvvvvvv', item);
				let _this = this;
				let id = aid;
				let url = file;
				// #ifdef APP-PLUS
			
				let files = uni.getStorageSync('files') ? JSON.parse(uni.getStorageSync('files')) : {}
				console.log(files)
				if (files[id]) {
					plus.runtime.openFile(files[id], {}, (res) => {
						console.log("res=>", res);
					})
				} else {
					downloadFile()
				}
			
				function downloadFile() {
					uni.showLoading({
						title: '加载中'
					})
					const dtask = plus.downloader.createDownload(url, {}, function(d, status) {
						if (status == 200) {
							console.log('保存成功')
							uni.hideLoading();
							files[id] = d.filename
							uni.setStorageSync('files', JSON.stringify(files))
							plus.runtime.openFile(d.filename)
						} else {
							plus.downloader.clear()
							uni.hideLoading();
							uni.showToast({
								title: '文档加载失败，请稍后再试',
								icon: 'none',
							})
						}
					})
					dtask.start()
				}
				// #endif
			
				// #ifdef H5
				window.open(url)
				// #endif
			},
			
			startclassFunc() {
				uni.navigateTo({
					url: `/pages/course/answer/answer?chapter_id=${this.idList[this.index]}&type=5`
				})
			},
			async get_course_chapterApi() {
				let res = await this.$api.post(global.apiUrls.post68cbcfecd48f6, {
					textbookchapter_id: +this.idList[this.index],
				})

				if (res.data.code == 1) {
					this.list = res.data.data.map(item=>{
						item.fileType = item.resource_file_path.slice(item.resource_file_path.lastIndexOf('.')).split('.')[1]
						return item
					})
				}
			},
			nextSection() {
				this.index++

				this.get_course_chapterApi()
			},
			goNext(val) {
				this.$urouter.navigateTo(
					`/pages/course/chapter/chapter?aid=${this.idList[this.index]}`
				);
			}
		}
	};
</script>

<style lang="scss" scoped>
	.public-page-empty {
		display: flex;
		justify-content: center;
		flex-direction: column;
		align-items: center;
		width: 100%;
		margin-top: 50rpx;

		image {
			width: 300rpx;
			height: 300rpx;
		}
	}

	.coursePro_flex_3 {
		width: 750rpx;
		height: 110rpx;
		overflow: hidden;
		z-index: 10;
		bottom: calc(0rpx + var(--window-bottom));
		background-color: #fff;

		.coursePro_fd3_0 {
			background: #145efd;
			width: 450rpx;
			height: 64rpx;
			line-height: 64rpx;
			font-size: 30rpx;
			color: #fff;
			border-radius: 44rpx;
			margin: 0 auto
		}
	}

	.page {
		width: 100vw;
		overflow-x: hidden;
		min-height: calc(100vh - var(--window-bottom));
		background: #F8F8F8;
		background-size: 100% auto;
		background-color: #F3F4F8;

	}

	.mance_flex_0 {
		background: #fff;
		width: 750rpx;
		height: 88rpx;
		overflow: hidden;
		z-index: 10;
		top: 0rpx;
		background-size: 100% auto !important;
	}

	.mance_fd0_0_c1_c0 {
		font-size: 36rpx;
		font-weight: 700;
		color: #333333;
		line-height: 50rpx;
	}

	.mance_fd0_0_c0_c0 {
		font-size: 36rpx;
		color: #333;
	}

	.mance_fd0_0_c0 {
		width: 120rpx;
	}

	.mance_fd0_0 {
		padding: 0rpx 32rpx 0rpx 32rpx;
	}

	.overview {
		min-height: 600rpx;
		background-color: #fff;
		width: 100%;
		height: 100%;
		padding: 0 24px;
		overflow-y: auto;
		margin-top: 95rpx;

		.title {
			margin-top: 30rpx;
			font-weight: 600;
			font-size: 30rpx;
			color: #131D34;
			display: flex;
			align-items: center;
			margin-bottom: 6rpx;

			.img-icon {
				width: 46rpx;
				height: 46rpx;
				margin-right: 10rpx;
			}
		}

		.resources-outer {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;

			.resources-item {
				width: 49%;
				height: 356rpx;
				background: #FFFFFF;
				box-shadow: 0rpx 3rpx 9rpx 1rpx rgba(0, 18, 80, 0.06);
				border-radius: 12rpx 12rpx 12rpx 12rpx;
				margin-top: 20rpx;
				position: relative;

				.item-img {
					width: 340rpx;
					height: 192rpx;
					border-radius: 16rpx 16rpx 0rpx 0rpx;
				}

				.type {
					width: 105rpx;
					height: 43rpx;
					position: absolute;
					top: 0;
					right: 0;
					font-weight: 400;
					font-size: 20rpx;
					color: #FFFFFF;
					line-height: 30rpx;
					text-align: center;
					background-image: url("@/static/img02/typebj.png");
					background-size: 105rpx 43rpx;
					background-repeat: no-repeat;
				}


				.title {
					width: 300rpx;
					height: 80rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					font-size: 28rpx;
					color: #131D34;
					line-height: 39rpx;
					text-align: left;
					font-style: normal;
					text-transform: none;
					margin-top: 16rpx;
					margin-left: 20rpx;
					overflow: hidden;

					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-line-clamp: 2;
					line-clamp: 2;
					-webkit-box-orient: vertical;
				}

				.jindu {
					font-weight: 400;
					font-size: 24rpx;
					color: #747E96;
					line-height: 33rpx;
					margin: 0 20rpx;

					span {
						font-weight: 400;
						font-size: 24rpx;
						color: #31CA91;
						margin-left: 12rpx;
					}
				}
			}
		}

		.cognition {
			display: flex;
			margin-left: 12rpx;
			padding-bottom: 32rpx;
			border-bottom: 1rpx solid #EEEEEE;
			margin-bottom: 32rpx;

			image {
				width: 36rpx;
				height: 36rpx;
			}

			.cognition-right {
				margin-left: 12rpx;

				.title-1 {
					font-weight: 500;
					font-size: 28rpx;
					color: #131D34;
					line-height: 40rpx;
				}

				.tag {
					margin-top: 24rpx;
					font-weight: 400;
					font-size: 24rpx;
					color: #FFFFFF;
					line-height: 33rpx;
					padding: 12rpx 30rpx;
					border-radius: 12rpx;
					width: fit-content;
				}
			}
		}

	}
</style>